package com.xhwl.logistics.service.business;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.xhwl.logistics.bo.input.QueryVehicleAccessRecordThirdBO;
import com.xhwl.logistics.converter.mapstruct.VehicleExcelConverter;
import com.xhwl.logistics.dao.mapper.IVehicleAccessRecordMapper;
import com.xhwl.logistics.entity.excel.VehicleAccessRecordExcel;
import com.xhwl.logistics.entity.excel.VehicleAccessReportFormsBiExcel;
import com.xhwl.logistics.po.VehicleAccessRecordSecondPO;
import com.xhwl.logistics.util.AboutDateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.OutputStream;
import java.util.Date;
import java.util.List;

//@Service
public class VehicleExcelBusinessService {

    @Autowired
    private IVehicleAccessRecordMapper vehicleAccessRecordMapper;

    @Autowired
    private VehicleExcelConverter vehicleExcelConverter;

    public void accessRecord(OutputStream outputStream, QueryVehicleAccessRecordThirdBO queryVehicleAccessRecordThirdBO) {
        try (ExcelWriter excelWriter = EasyExcel.write(outputStream, VehicleAccessRecordExcel.class).excelType(ExcelTypeEnum.XLSX).build()) {
            if (queryVehicleAccessRecordThirdBO.getMultiSheet()) {
                Date enterStartTime = queryVehicleAccessRecordThirdBO.getEnterStartTime();
                Date enterEndTime = queryVehicleAccessRecordThirdBO.getEnterEndTime();

                int number = 0;
                do {
                    Date start;
                    if (number == 0) {
                        start = AboutDateUtil.theSameMonthOfDate(enterEndTime);
                    } else {
                        start = AboutDateUtil.dateMinusMonth(enterEndTime, 1);
                    }
                    Date end = enterEndTime;

                    queryVehicleAccessRecordThirdBO.setEnterStartTime(start);
                    queryVehicleAccessRecordThirdBO.setEnterEndTime(end);

                    List<VehicleAccessRecordSecondPO> vehicleAccessRecordSecondPOList = vehicleAccessRecordMapper.listAccessRecords(queryVehicleAccessRecordThirdBO);
                    List<VehicleAccessRecordExcel> vehicleAccessRecordExcelList = vehicleExcelConverter.vehicleAccessRecordSecondPOList2VehicleAccessRecordExcelList(vehicleAccessRecordSecondPOList);

                    WriteSheet writeSheet = EasyExcel.writerSheet(number, AboutDateUtil.dateToString(start, "yyyy'年'MM'月'")).build();

                    excelWriter.write(vehicleAccessRecordExcelList, writeSheet);

                    number++;
                    enterEndTime = start;
                } while (enterStartTime.before(enterEndTime));
            } else {
                List<VehicleAccessRecordSecondPO> vehicleAccessRecordSecondPOList = vehicleAccessRecordMapper.listAccessRecords(queryVehicleAccessRecordThirdBO);
                List<VehicleAccessRecordExcel> vehicleAccessRecordExcelList = vehicleExcelConverter.vehicleAccessRecordSecondPOList2VehicleAccessRecordExcelList(vehicleAccessRecordSecondPOList);

                WriteSheet writeSheet = EasyExcel.writerSheet("车辆进出记录").build();
                excelWriter.write(vehicleAccessRecordExcelList, writeSheet);
            }
        }
    }

    public void accessReportFormsBi(OutputStream outputStream, List<VehicleAccessReportFormsBiExcel> vehicleAccessReportFormsBiExcels) {
        ExcelWriter excelWriter = EasyExcel.write(outputStream).excelType(ExcelTypeEnum.XLSX).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "车辆进出统计").head(VehicleAccessReportFormsBiExcel.class).build();
        excelWriter.write(vehicleAccessReportFormsBiExcels, writeSheet);
        excelWriter.finish();
    }

}
